using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using GonzalesCore.CoreElements;
using GonzalesCore.CoreManagers;
using Kypris2.CoreElements;
using Kypris2.Utils;
using Jamila2.Tools;

public partial class GonzalesClaimsPendingForFinanceApproval : System.Web.UI.Page
{
    #region Variables 

    protected PrivateLoginSession CurrentPrivateSession = null;
    protected LogManager CurLogManager = null;
    protected DataObj CurPageDataObj = new DataObj();

    #region ResultTable
    protected DataTable ResultTable
    {
        get
        {
            return (DataTable)ViewState[GonzalesCommonNamingConvention.CurrentPrivateSessionViewState];
        }

        set
        {
            ViewState[GonzalesCommonNamingConvention.CurrentPrivateSessionViewState] = value;
        }
    }
    #endregion

    #region CurrentPageIndex

    protected int CurrentPageIndex
    {
        get
        {
            return (Int32)ViewState["CurrentPageIndex"];
        }

        set
        {
            ViewState["CurrentPageIndex"] = value;
        }
    }

    #endregion

    #region Common Variables for Language Translation


    #region Item_DateAuthorized
    public string Item_DateAuthorized
    {
        get
        {
            return ViewState["Item_DateAuthorized"].ToString();
        }

        set
        {
            ViewState["Item_DateAuthorized"] = value;
        }
    }
    #endregion

    #region Item_ClaimID
    public string Item_ClaimID
    {
        get
        {
            return ViewState["Item_ClaimID"].ToString();
        }

        set
        {
            ViewState["Item_ClaimID"] = value;
        }
    }
    #endregion

    #region Item_Purpose
    public string Item_Purpose
    {
        get
        {
            return ViewState["Item_Purpose"].ToString();
        }

        set
        {
            ViewState["Item_Purpose"] = value;
        }
    }
    #endregion

    #region Item_ClaimPeriod
    public string Item_ClaimPeriod
    {
        get
        {
            return ViewState["Item_ClaimPeriod"].ToString();
        }

        set
        {
            ViewState["Item_ClaimPeriod"] = value;
        }
    }
    #endregion

    #region Item_LocalCurrency
    public string Item_LocalCurrency
    {
        get
        {
            return ViewState["Item_LocalCurrency"].ToString();
        }

        set
        {
            ViewState["Item_LocalCurrency"] = value;
        }
    }
    #endregion

    #region Item_LocalAmount
    public string Item_LocalAmount
    {
        get
        {
            return ViewState["Item_LocalAmount"].ToString();
        }

        set
        {
            ViewState["Item_LocalAmount"] = value;
        }
    }
    #endregion

    #region Item_Claimant
    public string Item_Claimant
    {
        get
        {
            return ViewState["Item_Claimant"].ToString();
        }

        set
        {
            ViewState["Item_Claimant"] = value;
        }
    }
    #endregion

    #region Item_Category
    public string Item_Category
    {
        get
        {
            string text = (string)ViewState["Item_Category"];
            if (text != null)
                return text;
            else
                return string.Empty;
        }

        set
        {
            ViewState["Item_Category"] = value;
        }
    }
    #endregion    

    #region SortByASC
    protected string SortByASC
    {
        get
        {
            return ViewState["SortByASC"].ToString();
        }

        set
        {
            ViewState["SortByASC"] = value;
        }
    }
    #endregion

    #region SortByDESC
    protected string SortByDESC
    {
        get
        {
            return ViewState["SortByDESC"].ToString();
        }

        set
        {
            ViewState["SortByDESC"] = value;
        }
    }
    #endregion

    #endregion

    #endregion

    #region Events

    protected void Page_Load(object sender, EventArgs e)
    {
        this.SessionCheck();

        if (!IsPostBack)
        {
            this.CurrentPageIndex = 0;

            this.SwitchLanguage();
            this.PageComponentSetup();

            this.LoadData();

            if (this.ResultTable != null)
            {
                DataView ResultView = new DataView(this.ResultTable);
                ResultView.Sort = this.DropDownList_SortBy.SelectedValue + " " + this.DropDownList_SortOrder.SelectedValue;
                this.BindData(ResultView);
            }
        }
    }

    protected void DropDownList_SortBy_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.ResultTable != null)
        {
            DataView ResultView = new DataView(this.ResultTable);
            ResultView.Sort = this.DropDownList_SortBy.SelectedValue + " " + this.DropDownList_SortOrder.SelectedValue;
            this.BindData(ResultView, this.CurrentPageIndex);
        }
    }

    protected void DropDownList_SortOrder_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.DropDownList_SortBy_SelectedIndexChanged(sender, e);
    }

    protected void GridViewResult_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.CurrentPageIndex = e.NewPageIndex;
        this.DropDownList_SortBy_SelectedIndexChanged(sender, e);
    }

    #endregion

    #region Customized Methods

    #region SessionCheck
    public void SessionCheck()
    {

        if (Session[GonzalesCommonNamingConvention.CurrentPrivateSession] != null)
        {
            this.CurrentPrivateSession = (PrivateLoginSession)Session[GonzalesCommonNamingConvention.CurrentPrivateSession];
            this.CurLogManager = new LogManager(this.CurrentPrivateSession);    // activate Log Manager
        }
        else
        {
            Response.Redirect(GonzalesErrorPageRedirect.SessionError);
        }

    }
    #endregion

    #region PageComponentSetup
    protected void PageComponentSetup()
    {
        // sort by
        this.DropDownList_SortBy.Items.Add(new ListItem(this.Item_DateAuthorized, this.CurPageDataObj.DateVerification));
        this.DropDownList_SortBy.Items.Add(new ListItem(this.Item_Category, this.CurPageDataObj.Category));
        this.DropDownList_SortBy.Items.Add(new ListItem(this.Item_ClaimID, this.CurPageDataObj.ClaimID));
        this.DropDownList_SortBy.Items.Add(new ListItem(this.Item_Purpose, this.CurPageDataObj.Purpose));
        this.DropDownList_SortBy.Items.Add(new ListItem(this.Item_ClaimPeriod, this.CurPageDataObj.ClaimPeriod));
        this.DropDownList_SortBy.Items.Add(new ListItem(this.Item_LocalCurrency, this.CurPageDataObj.LocalCurrency));
        this.DropDownList_SortBy.Items.Add(new ListItem(this.Item_LocalAmount, this.CurPageDataObj.LocalAmount));
        this.DropDownList_SortBy.Items.Add(new ListItem(this.Item_Claimant, this.CurPageDataObj.Claimant));

        // sort order
        this.DropDownList_SortOrder.Items.Add(new ListItem(this.SortByASC, "ASC"));
        this.DropDownList_SortOrder.Items.Add(new ListItem(this.SortByDESC, "DESC"));

        // GridView
        this.GridViewResult.Columns[0].HeaderText = this.Item_DateAuthorized;
        this.GridViewResult.Columns[1].HeaderText = this.Item_Category;
        this.GridViewResult.Columns[2].HeaderText = this.Item_ClaimID;
        this.GridViewResult.Columns[3].HeaderText = this.Item_Purpose;
        this.GridViewResult.Columns[4].HeaderText = this.Item_ClaimPeriod;
        this.GridViewResult.Columns[5].HeaderText = this.Item_LocalCurrency;
        this.GridViewResult.Columns[6].HeaderText = this.Item_LocalAmount;
        this.GridViewResult.Columns[7].HeaderText = this.Item_Claimant;


    }
    #endregion

    #region SwitchLanguage
    protected void SwitchLanguage()
    {
        try
        {
            using (MultiLingualFactory CurLanguageFactory = MultiLingualEngine.GetMultiLingualFactory(this, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.InternalID))
            {
                this.Label_Information.Text = CurLanguageFactory.DisplayValue("Information");
                this.Label_PageTitle.Text = CurLanguageFactory.DisplayValue("PageTitle");
                this.Label_Remarks.Text = CurLanguageFactory.DisplayValue("Remarks");

                this.Item_DateAuthorized = CurLanguageFactory.DisplayValue("ItemDateVerified");
                this.Item_ClaimID = CurLanguageFactory.DisplayValue("ItemClaimID");
                this.Item_Purpose = CurLanguageFactory.DisplayValue("ItemPurpose");
                this.Item_ClaimPeriod = CurLanguageFactory.DisplayValue("ItemClaimPeriod");
                this.Item_LocalCurrency = CurLanguageFactory.DisplayValue("ItemCurrency");
                this.Item_LocalAmount = CurLanguageFactory.DisplayValue("ItemAmount");
                this.Item_Claimant = CurLanguageFactory.DisplayValue("ItemClaimant");
                this.Item_Category = CurLanguageFactory.DisplayValue("ItemCategory");

                this.SortByASC = CurLanguageFactory.DisplayValue("SortOrderASC");
                this.SortByDESC = CurLanguageFactory.DisplayValue("SortOrderDESC");

            }
        }
        catch (Exception ex)
        {
            this.CurLogManager.LogError("[GonzalesClaimsPendingForVerification.SwitchLanguage.EX] : " + ex.Message, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.User);
        }

    }
    #endregion

    #endregion

    #region Data Operation

    protected void LoadData()
    {
        using (ClaimManager CurClaimManager = new ClaimManager(this.CurrentPrivateSession))
        {
            ClaimHeaders ResultObj = CurClaimManager.GetClaimsByFinanceApprover(this.CurrentPrivateSession.CurrentLoggedOnUser.InternalID);

            if (ResultObj != null)
            {
                this.ResultTable = new DataTable();
                ResultTable.Columns.Add(new DataColumn(CurPageDataObj.DateVerification, Type.GetType("System.DateTime")));
                ResultTable.Columns.Add(new DataColumn(CurPageDataObj.DateVerificationInStr, Type.GetType("System.String")));
                ResultTable.Columns.Add(new DataColumn(CurPageDataObj.Category, Type.GetType("System.String")));
                ResultTable.Columns.Add(new DataColumn(CurPageDataObj.ClaimID, Type.GetType("System.String")));
                ResultTable.Columns.Add(new DataColumn(CurPageDataObj.Purpose, Type.GetType("System.String")));
                ResultTable.Columns.Add(new DataColumn(CurPageDataObj.LocalCurrency, Type.GetType("System.String")));
                ResultTable.Columns.Add(new DataColumn(CurPageDataObj.LocalAmount, Type.GetType("System.String")));
                ResultTable.Columns.Add(new DataColumn(CurPageDataObj.ClaimPeriod, Type.GetType("System.String")));
                ResultTable.Columns.Add(new DataColumn(CurPageDataObj.Claimant, Type.GetType("System.String")));
                ResultTable.Columns.Add(new DataColumn(CurPageDataObj.StatusInStr, Type.GetType("System.String")));

                DataRow RowObj = null;

                for (int x = 0; x < ResultObj.Count; x++)
                {
                    RowObj = ResultTable.NewRow();

                    GonzalesClaimHeader CurClaimHeader = (GonzalesClaimHeader)ResultObj[x];

                    RowObj[CurPageDataObj.DateVerification] = ResultObj[x].SubmittedOn;
                    RowObj[CurPageDataObj.DateVerificationInStr] = "<a href='GonzalesClaimsPendingForFinanceApprovalDetail.aspx?id=" + ResultObj[x].InternalID + "' class='GridViewLink'>" + SwissArmy.DateConvert(ResultObj[x].SubmittedOn, this.CurrentPrivateSession.DateFormatInUse, "/") + "</a>";
                    RowObj[CurPageDataObj.Category] = ClaimManager.GetClaimCategoryInString(CurClaimHeader.Category);
                    RowObj[CurPageDataObj.ClaimID] = ResultObj[x].ClaimID;
                    RowObj[CurPageDataObj.Purpose] = ResultObj[x].Reasons;
                    RowObj[CurPageDataObj.LocalCurrency] = CurClaimHeader.LocalCurrency.InternalID;
                    RowObj[CurPageDataObj.LocalAmount] = ResultObj[x].TotalClaimedAmount.ToString(GonzalesGeneralVariables.CurrencyFormat); ;
                    if (ResultObj[x].ProjectedOn != DateTime.MinValue)
                        RowObj["ClaimPeriod"] =ResultObj[x].ProjectedOn.Year.ToString() + " - " + ResultObj[x].ProjectedOn.Month.ToString();// ResultObj[x].ProjectedOn.ToString("MMMM") + " " + ResultObj[x].ProjectedOn.Year.ToString();
                    else
                        RowObj["ClaimPeriod"] = "";
                    RowObj[CurPageDataObj.Claimant] = ResultObj[x].ClaimedBy.DisplayName + " (" + ResultObj[x].ClaimedBy.EmailID + ")";
                    RowObj[CurPageDataObj.StatusInStr] = ClaimManager.GetClaimStatusInString(CurClaimHeader.Status);

                    ResultTable.Rows.Add(RowObj);
                }

            }
        }
    }

    private void BindData(DataView result)
    {
        this.GridViewResult.DataSource = result;
        this.GridViewResult.DataBind();
    }

    private void BindData(DataView result, int pageindex)
    {
        this.GridViewResult.PageIndex = pageindex;
        this.GridViewResult.DataSource = result;
        this.GridViewResult.DataBind();
    }

    #endregion

    #region Class: DataObj

    protected class DataObj
    {
        public readonly string InternalID = "InternalID";
        public readonly string DateVerification = "DateVerification";
        public readonly string DateVerificationInStr = "DateVerificationInStr";
        public readonly string Category = "Category";
        public readonly string ClaimID = "ClaimID";
        public readonly string Purpose = "Purpose";
        public readonly string LocalCurrency = "Currency";
        public readonly string LocalAmount = "LocalAmount";
        public readonly string Claimant = "Claimant";
        public readonly string ClaimPeriod = "ClaimPeriod";
        public readonly string StatusInStr = "StatusInStr";
    }

    #endregion
}
